package com.annoncesautomobile.vue.annuairepro;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.concreteEntities.Client;
import com.annoncesautomobile.vue.utils.ImageLoader;

public class ListViewAnnuaireProAdapter extends BaseAdapter {
	
	private AnnuaireProActivity activity;
	private List<Client> listClients;
	private Client clientMoreClients;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader; 
	
	public ListViewAnnuaireProAdapter(AnnuaireProActivity activity, List<Client> listClients, Client clientMoreClients) {
		this.activity = activity;
		this.listClients = listClients;
		this.clientMoreClients = clientMoreClients;
		inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.imageLoader = new ImageLoader(this.activity.getApplicationContext());
	}

	@Override
	public int getCount() {
		return this.listClients.size();
	}

	@Override
	public Object getItem(int position) {
		return this.listClients.get(position);
	}

	@Override
	public long getItemId(int position) {
		return this.listClients.get(position).getIdClient();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Client client = this.listClients.get(position);
		View view = null;

		if(client == this.clientMoreClients) {
			this.activity.loadMoreData();
			view = inflater.inflate(R.layout.list_row_more, null);
			TextView texte = (TextView)view.findViewById(R.id.texte);
			texte.setText("Cliquer ici pour charger plus de professionnels...");
		} else {
			view = inflater.inflate(R.layout.list_row_client_pro, null);

			TextView nomClientPro = (TextView)view.findViewById(R.id.nomClientPro);
			TextView textClientPro = (TextView)view.findViewById(R.id.textClientPro);
			ImageView thumb_image=(ImageView)view.findViewById(R.id.image);

			nomClientPro.setText(client.getNom());
			String text = client.getAdresse()+"\n"+client.getCp()+" "+client.getVille()+"\n"+client.getDepartement()+" ("+client.getDepartement_num()+")";
			textClientPro.setText(text);
			
			imageLoader.DisplayImage(client.getImage(), thumb_image);
		}
		return view;
	}

}
